package com.useeinfo.oa.modules.repair.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.CommonUtils;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.DictInfoBiz;
import com.useeinfo.oa.modules.base.biz.FacilityCatBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.FacilityCat;
import com.useeinfo.oa.modules.base.entity.Personnel;
import com.useeinfo.oa.modules.repair.dao.TroubleCatDao;
import com.useeinfo.oa.modules.repair.entity.TroubleCat;
import com.useeinfo.oa.modules.security.biz.OrganizationBiz;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.List;

/**
 * @Date: 2018/12/6 18:50
 * @Description: 故障分类
 */
@Service
public class TroubleCatBiz extends CrudBiz<TroubleCatDao, TroubleCat> {
    @Autowired
    private OrganizationBiz organizationBiz;
    @Autowired
    private DictInfoBiz dictInfoBiz;
    @Autowired
    private FacilityCatBiz facilityCatBiz;
    @Autowired
    private HttpServletRequest request;

    //保存故障非类
    public ResultDto saveUpdate(TroubleCat troubleCat, HttpServletRequest request, List<String> organizations, List<String> facilityCats, List<String> repairClassifys) {
        try {
            Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);

            if (enterprise == null) {
                return ResultUtil.error("登陆过期，请重新登录");
            }
            troubleCat.setEnterprise(enterprise);
            if (CollectionUtils.isNotEmpty(organizations)) {
                troubleCat.setOrganization(organizationBiz.findModel(StringConverters.ToLong(organizations.get(0))));
                troubleCat.setOrganizationName(organizationBiz.findModel(StringConverters.ToLong(organizations.get(0))).getOrganizationName());
            }
            if (CollectionUtils.isNotEmpty(repairClassifys)) {
                troubleCat.setReportClassify(dictInfoBiz.findModel(StringConverters.ToLong(repairClassifys.get(0))));
                troubleCat.setClassify(dictInfoBiz.findModel(StringConverters.ToLong(repairClassifys.get(0))).getPropertyName());
            }
            if (CollectionUtils.isNotEmpty(facilityCats)) {
                troubleCat.setFacilityCat(facilityCatBiz.findModel(StringConverters.ToLong(facilityCats.get(0))));
                troubleCat.setMajor(facilityCatBiz.findModel(StringConverters.ToLong(facilityCats.get(0))).getCatName());
            }
            troubleCat.setPoints(troubleCat.getPoints()==null?0:troubleCat.getPoints());
            if (troubleCat.getId() == null) {
                //新增时生成流水号，随机10位数字
                troubleCat.setSerial(CommonUtils.getRandomNumber(10));
                dao.add(troubleCat);
            } else {
                dao.update(troubleCat);

            }
            return ResultUtil.success();
        } catch (Exception e) {
            logger.info("保存故障非类出错，错误信息：{}", e.getMessage());
            return ResultUtil.error();
        }

    }

    public ResultDto troubleCatList() {
        QueryParam queryParam = new QueryParam(-1,-1);
        if(SessionKeyUtils.getSessionEnterprise(request)!=null){
            queryParam.getSqlMap().put("enterpriseId",SessionKeyUtils.getSessionEnterprise(request).getId().toString());
        }
        List<TroubleCat> troubleCats = dao.findList(queryParam);
        JSONArray jsonArray = new JSONArray();
        JSONObject result=new JSONObject();
        troubleCats.forEach(x -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("troubleCatId", x.getId());
            jsonObject.put("troubleCatName", x.getTroubleCatName());
            jsonArray.add(jsonObject);
        });
        result.put("rows",jsonArray);

        return ResultUtil.success(result);
    }

}
